<script>
	dojo.addOnLoad( function( ) {
		var combo = new dijit.form.ComboBox( {
            id: "candidatesList",
            name: "userLogin",
            store: dijit.byId( "activeGrid").store,
            searchAttr: "userLogin"
        },
        "candidatesList");
	});


	function candidatesCancel( event) {
		dijit.byId( "addPersonalDialog").destroyRecursive( );
	}

	function candidateSubmit( ) {
		var item = 
		dojo.xhrPost( {
			url 	: "/FinalProject/app/projectManagement",
			content : { 
				projectOp 	: "project_add_personal", 
				projectName	: dijit.byId( "projectsGrid").selection.getSelected()[0].projectName, 
				userLogin 	: dijit.byId( "candidatesList").attr( "value")},
			load	: function( data, ioArgs) {
				if ( data == "ERROR") {
			        dijit.byId('toaster').setContent('Backend error. Could not add personal to project.', 'fatal');
			        dijit.byId('toaster').show();							
					return;
				}
				dijit.byId( "activeGrid").store.fetch( {
					query 		: { userLogin : dijit.byId( "candidatesList").attr( "value")},
					onComplete 	: function( items, result) {
						dojo.forEach( items, function(item) {
							dijit.byId( "personalGrid").store.newItem({ userLogin 	: item.userLogin[0],
							  firstName 	: item.firstName[0],
							  lastName 		: item.lastName[0],
							  phoneNumber	: item.phoneNumber[0],
							  role			: item.role[0],
							  activityStatus: item.activityStatus[0]});	
						});
					} 
				});
				
		        dijit.byId('toaster').setContent('Personal added successefuly.', 'fatal');
		        dijit.byId('toaster').show();
		        dijit.byId( "addPersonalDialog").destroyRecursive( );						
			},
			error	: function( ioArgs) {
		        dijit.byId('toaster').setContent('Backend error. Could not add personal to project, please try later.', 'fatal');
		        dijit.byId('toaster').show();
		        dijit.byId( "addPersonalDialog").destroyRecursive( );					
			}
		});
	}
</script>
<div dojoType="dijit.layout.ContentPane">
	<form>
		<fieldset>
			<table>
				<tr>
					<td align="left">
						<label>
							Please select employee from the list
						</label>
					</td>
					<td align="right">
						<input id="candidatesList" />
					</td>
				</tr>
				<tr>
					<td colspan=2 align="right">
						<button onclick="candidatesCancel( );" dojoType="dijit.form.Button">Cancel</button>
						<button onclick="candidateSubmit( );" dojoType="dijit.form.Button">Select</button>						
					</td>
				</tr>
			</table>
		</fieldset>
	</form>
</div>